package com.lxmes.gs.service.impl;

import java.util.List;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.CostSummaryMapper;
import com.lxmes.gs.domain.CostSummary;
import com.lxmes.gs.service.ICostSummaryService;

/**
 * 成本统计Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class CostSummaryServiceImpl extends ServiceImpl<CostSummaryMapper,CostSummary> implements ICostSummaryService{
    
    @Autowired
    private CostSummaryMapper costSummaryMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询成本统计
     * 
     * @param id 成本统计主键
     * @return 成本统计
     */
    @Override
    public CostSummary selectCostSummaryById(Long id){
        return costSummaryMapper.selectById(id);
    }

    /**
     * 查询成本统计列表
     * 
     * @param costSummary 成本统计
     * @return 成本统计
     */
    @Override
    public List<CostSummary> selectCostSummaryList(CostSummary costSummary){
        return costSummaryMapper.selectList(Wrappers.lambdaQuery(costSummary));
    }

    /**
     * 新增成本统计
     * 
     * @param costSummary 成本统计
     * @return 结果
     */
    @Override
    public int insertCostSummary(CostSummary costSummary){
        insertOrUpdate(costSummary);
        return costSummaryMapper.insert(costSummary);
    }

    private void insertOrUpdate(CostSummary costSummary) {

        Double materialCost = ObjectUtil.defaultIfNull(costSummary.getMaterialCost(),0D);
        Double laborCost = ObjectUtil.defaultIfNull(costSummary.getLaborCost(),0D);
        Double leasingCost = ObjectUtil.defaultIfNull(costSummary.getLeasingCost(),0D);
        Double subcontractingCost = ObjectUtil.defaultIfNull(costSummary.getSubcontractingCost(),0D);
        Double indirectCost = ObjectUtil.defaultIfNull(costSummary.getIndirectCost(),0D);
        costSummary.setTotalCost(NumberUtil.add(materialCost,laborCost,leasingCost,subcontractingCost,indirectCost).doubleValue());
        
    }

    /**
     * 修改成本统计
     * 
     * @param costSummary 成本统计
     * @return 结果
     */
    @Override
    public int updateCostSummary(CostSummary costSummary){
        insertOrUpdate(costSummary);
        return costSummaryMapper.updateById(costSummary);
    }

    /**
     * 批量删除成本统计
     * 
     * @param ids 需要删除的成本统计主键
     * @return 结果
     */
    @Override
    public int deleteCostSummaryByIds(Long[] ids){
        return costSummaryMapper.deleteCostSummaryByIds(ids);
    }

    /**
     * 删除成本统计信息
     * 
     * @param id 成本统计主键
     * @return 结果
     */
    @Override
    public int deleteCostSummaryById(Long id){
        return costSummaryMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<CostSummary> costSummaryList, boolean updateSupport) {
        if (StringUtils.isNull(costSummaryList) || costSummaryList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CostSummary costSummary : costSummaryList)
        {
            try
            {
                BeanValidators.validateWithException(validator, costSummary);
                this.insertCostSummary(costSummary);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
